package com.cz.ssyx.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.sys.RegionWare;
import com.cz.ssyx.sys.service.RegionWareService;
import com.cz.ssyx.vo.sys.RegionWareQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 城市仓库关联表 前端控制器
 * </p>
 *
 * @author Joker
 * @since 2024-04-17
 */

@Api(tags = "开通区域接口")
//@CrossOrigin
@RestController
@RequestMapping("/admin/sys/regionWare")
public class RegionWareController {

    @Autowired
    private RegionWareService regionWareService;

    //1.查看开通区域列表
    @ApiOperation("查看开通区域列表")
    @GetMapping("/{page}/{limit}")
    public Result getPageList(@PathVariable Long page, @PathVariable Long limit,
                              RegionWareQueryVo regionWareQueryVo) {
        Page<RegionWare> regionWarePage = new Page<>(page, limit);

        LambdaQueryWrapper<RegionWare> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(regionWareQueryVo.getKeyword())) {
            queryWrapper.like(RegionWare::getRegionName, regionWareQueryVo.getKeyword())
                    .or().like(RegionWare::getWareName, regionWareQueryVo.getKeyword());
        }
        Page<RegionWare> resultPage = regionWareService.page(regionWarePage, queryWrapper);
        return Result.ok(resultPage);
    }

    //新增开通区域
    @ApiOperation("新增开通区域")
    @PostMapping("/save")
    public Result save(@RequestBody RegionWare regionWare) {
        regionWareService.saveRegionWare(regionWare);
        return Result.ok(null);
    }

    @ApiOperation("删除开通区域")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        regionWareService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("取消开通区域")
    @PostMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable int status) {
//        LambdaUpdateWrapper<RegionWare> updateWrapper = new LambdaUpdateWrapper<>();
//        updateWrapper.eq(RegionWare::getId, id).set(RegionWare::getStatus, status);
//        regionWareService.update(updateWrapper);
        RegionWare regionWare = regionWareService.getById(id);
        regionWare.setStatus(status);
        regionWareService.updateById(regionWare);
        return Result.ok(null);
    }
}

